<?php
namespace Favorite\Controller;
use Common\Controller\CommonController;

/**
 * 收藏模块
 */
class FavoriteController extends CommonController {
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_auth();
		
		$this->_moduleid = 104;
		$this->_favorite = new \Common\Model\FavoriteModel(); 
	}
    
    
    
	/**
	 * 添加收藏
	 */
	public function addFavoriteAction(){
		$moduleid = (int)I('request.moduleid');
		$fcustid = (int)I('request.fcustid');
		$title = I('request.title');
		$author = I('request.author');
		$content = I('request.content');
		$url = I('request.url');
		$imgurl = I('request.imgurl');
		$targetid = (int)I('request.targetid');
		
		$this->_addFavorite($moduleid , $fcustid , $title , $author , $content , $url , $imgurl , $targetid);
	}
	
	private function _addFavorite($moduleid , $fcustid , $title , $author , $content , $url , $imgurl , $targetid){
		if(empty($moduleid) || empty($fcustid) || (empty($title) && empty($content) && empty($url) && empty($imgurl) && empty($targetid))){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['favoritehash'] = md5($title . $content . $url . $imgurl . $targetid . $moduleid . $fcustid);
		$favoriteInfo = $this->_favorite->getFavoriteByHash($data);
		if(!empty($favoriteInfo)){
			$this->status = 1500;
			$this->msg = L('STATUS_1500');
			$this->outPut();
		}
		
		$data['fcustid'] = $fcustid;
		$data['title'] = $title;
		$data['author'] = $author;
		$data['content'] = $content;
		$data['url'] = $url;
		$data['imgurl'] = $imgurl;
		$data['targetid'] = $targetid;
		$data['moduleid'] = $moduleid;
		$ret = $this->_favorite->addFavorite($data);
		if($ret){
			$this->outPut();
		}else{
			$this->status = 1501;
			$this->msg = L('STATUS_1501');
			$this->outPut();
		}
	}
    
	/**
	 * 删除收藏
	 */
	public function deleteFavoriteAction(){
		$favoriteid = (int)I('request.favoriteid');
		$this->_deleteFavorite($favoriteid);
	}
	
	private function _deleteFavorite($favoriteid){
		if(empty($favoriteid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$data['favoriteid'] = $favoriteid;
		$data['custid'] = $this->_custInfo['custid'];
		$ret = $this->_favorite->deleteFavorite($data);
		if($ret){
			$this->outPut();
		}else{
			$this->status = 1503;
			$this->msg = L('STATUS_1503');
			$this->outPut();
		}
	}
	
	/**
	 * 我的收藏
	 */
	public function myFavoriteListAction(){
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_myFavoriteList($pageIndex , $pageSize);
	}
	
	private function _myFavoriteList($pageIndex = 0 , $pageSize = 0){
    	if($pageIndex)$pageIndex--;
    	$data['pageIndex'] = $pageIndex;
    	$data['pageSize'] = $pageSize;
    	
    	$data['custid'] = $this->_custInfo['custid'];
    	$result = $this->_favorite->getFavoriteList($data);
    	if(!empty($result['data'])){
    		foreach($result['data'] as &$favorite){
    			$custInfo = $this->_getCustInfo($favorite['fcustid']);
    			$favorite['nickname'] = $custInfo['nickname'];
    		}
    		
    		$this->data = $result['data'];
    		$this->outPut();
    	}else{
    		$this->status = 1505;
    		$this->msg = L('STATUS_1505');
    		$this->outPut();
    	}
	}
	
    
}
